package seedpod.kb2db.protegeplugin;
/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import javax.swing.JEditorPane;

public class PromptSaveUponDocumentCloseListener extends WindowAdapter
{

	private JEditorPane _editor;
	private File _destFile;

	public PromptSaveUponDocumentCloseListener(JEditorPane editor, File destFile) {
		_editor = editor;
		_destFile = destFile;
	}

	public void windowClosing( WindowEvent e ) {
		File _exportFile = Protege2SQLExportPlugin.promptForFile( _destFile.getName() );
		if ( _exportFile != null ) {
			saveFile( _exportFile, _editor.getText() );
		}
	}

	private static void saveFile( File file, String content ) {
		try {
			PrintWriter tmpWriter = new PrintWriter( file );
			tmpWriter.write( content );
			tmpWriter.flush();
			tmpWriter.close();
		}
		catch (FileNotFoundException fe) {
			// DO NOTHING
		}
	}
}
